<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var layer; 

    function test_Layer_FixedZoomLevels (t) {
        t.plan( 39 );
        
        var layer = { 'MIN_ZOOM_LEVEL': 5,
                      'MAX_ZOOM_LEVEL': 10 };        


    //defaults 

        layer = p_createLayer(layer);
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "nothing specified");

    
    //layer.options

        // min,num
        layer = p_createLayer(layer, {}, { minZoomLevel: 3, numZoomLevels: 12});
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "min too low num too high(layer.options)");

        layer = p_createLayer(layer, {}, { minZoomLevel: 6, numZoomLevels: 3 });
        p_minMaxNum(t, layer, 6, 8, "valid min,num(layer.options)");


        // max
        layer = p_createLayer(layer, {}, { maxZoomLevel: 9 });
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, 9, "valid max(layer.options)");

        layer = p_createLayer(layer, {}, { maxZoomLevel: 12 });
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "invalid max(layer.options)");



    //map

        // min,num
        layer = p_createLayer(layer, { minZoomLevel: 3, numZoomLevels: 12});
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "min too low num too high(map)");

        layer = p_createLayer(layer, { minZoomLevel: 6, numZoomLevels: 3 });
        p_minMaxNum(t, layer, 6, 8, "valid min,num(map)");


        // max
        layer = p_createLayer(layer, { maxZoomLevel: 9 });
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, 9, "valid max(map)");

        layer = p_createLayer(layer, { maxZoomLevel: 12 });
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "invalid max(map)");

    //map vs. options

        layer = p_createLayer(layer, {minZoomLevel: 6, numZoomLevels: 2}, { minZoomLevel: 7, numZoomLevels: 3});
        p_minMaxNum(t, layer, 7, 9, "min,num(layer.options) wins over (map)");

        layer = p_createLayer(layer, {minZoomLevel: 6, maxZoomLevel: 8}, { minZoomLevel: 7, maxZoomLevel: 9});
        p_minMaxNum(t, layer, 7, 9, "min,max(layer.options) wins over (map)");


    // numZoomLevels vs. maxZoomLevel
    
        layer = p_createLayer(layer, {maxZoomLevel: 8, numZoomLevels: 6});
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, 10, "min,max(layer.options) wins over (map)");

    // resolutions array

        var resolutions = Array(20);
        for (var i = 0; i < 20; i++) {
            resolutions[i] = Math.random();
        }
        OpenLayers.Util.extend(layer, {RESOLUTIONS:resolutions});
        var minZoomLevel = 6;
        var numZoomLevels = 2;
        layer = p_createLayer(layer, {}, {minZoomLevel: minZoomLevel, numZoomLevels: numZoomLevels});
        t.eq( layer.resolutions.length, numZoomLevels, "length of resolutions array ok");
        for (var i = 0; i < numZoomLevels; i++) {
            t.eq( layer.resolutions[i], resolutions[i + minZoomLevel], "resolutions array at index " + i + " ok");
        }
    }
    
    function test_getMapObjectZoomFromOLZoom(t) {
        t.plan(4);
        
        var map = new OpenLayers.Map("map", {allOverlays: true});
        var xyz = new OpenLayers.Layer.XYZ("xyz", "${x}${y}${z}", {
            sphericalMercator: true,
            resolutions: [39135.7584765625, 19567.87923828125, 9783.939619140625]
        });
        var fixed = new (OpenLayers.Class(OpenLayers.Layer, OpenLayers.Layer.FixedZoomLevels, {
             initialize: function() {
                OpenLayers.Layer.prototype.initialize.apply(this, arguments);
            }
        }))("fixed", {
            resolutions: [156543.03390625, 78271.516953125, 39135.7584765625, 19567.87923828125, 9783.939619140625],
            minZoomLevel: 1
        });
        map.addLayers([xyz, fixed]);
        map.setCenter(new OpenLayers.LonLat(0, 0), 2);
        // map.getZoom() returns 2
        t.eq(fixed.getMapObjectZoomFromOLZoom(map.getZoom()), 4, "correct return value from getMapObjectZoomFromOLZoom");
        t.eq(fixed.getOLZoomFromMapObjectZoom(4), map.getZoom() - fixed.minZoomLevel, "correct return value from getOLZoomFromMapObjectZoom");

        map.setBaseLayer(fixed);
        // map.getZoom() returns 4 now
        t.eq(fixed.getMapObjectZoomFromOLZoom(map.getZoom()), 5, "correct return value from getMapObjectZoomFromOLZoom");
        t.eq(fixed.getOLZoomFromMapObjectZoom(5), map.getZoom(), "correct return value from getOLZoomFromMapObjectZoom");
    }

    function p_createLayer(layer, mapOptions, layerOptions) {

        layer.map = mapOptions || {};
        layer.options = layerOptions || {};
        OpenLayers.Layer.FixedZoomLevels.prototype.initResolutions.apply(layer);

        return layer;
    }

    function p_minMaxNum(t, layer, min, max, msg) {

        t.eq( layer.minZoomLevel, min, "min zoom level inherited from layer constant: " + msg);
        t.eq( layer.maxZoomLevel, max, "max zoom level inherited from layer constant: " + msg);
        t.eq( layer.numZoomLevels, max - min + 1, "num zoom levels correctly calcuated: " + msg);
        
    }


  </script>
</head>
<body>
  <div id="map" style="width:256px;height:256px"></div>
</body>
</html>
